/***********************************************************************
    filename:   CEGUIGeometryBuffer.h
    created:    Mon Feb 9 2009
    author:     Paul D Turner
*************************************************************************/

#ifndef _hgeGeometryBuffer_h_
#define _hgeGeometryBuffer_h_

#include "hgeRect.h"
#include "hgeRenderer.h"
#include <d3dx9.h>
#include <utility>
#include <vector>

#if defined(_MSC_VER)
#   pragma warning(push)
#   pragma warning(disable : 4251)
#endif

/*!
\brief
    Direct3D9 based implementation of the GeometryBuffer interface.
*/
class GeometryBuffer
{
public:
    //! Constructor
    GeometryBuffer(LPDIRECT3DDEVICE9 device);

    //! return pointer to D3DXMATRIX used as world transform.
    const D3DXMATRIX* getMatrix() const;

    // implementation of abstract members from GeometryBuffer
    void draw() const;
    void setTranslation(const Vector3& t);
    void setRotation(const Vector3& r);
    void setPivot(const Vector3& p);
    void setClippingRegion(const hgeRect& region);
    void appendVertex(const Vertex& vertex);
    void appendGeometry(const Vertex* const vbuff, UINT vertex_count);
    void setActiveTexture(Texture* texture);
    void reset();
    Texture* getActiveTexture() const;
    UINT getVertexCount() const;
    UINT getBatchCount() const;
    void setRenderEffect(RenderEffect* effect);
    RenderEffect* getRenderEffect();

protected:
    //! perform batch management operations prior to adding new geometry.
    void performBatchManagement();
    //! update cached matrix
    void updateMatrix() const;

    //! internal Vertex structure used for Direct3D based geometry.
    struct D3DVertex
    {
        //! The transformed position for the vertex.
        FLOAT x, y, z;
        //! colour of the vertex.
        DWORD diffuse;
        //! texture coordinates.
        float tu, tv;
    };

    //! last texture that was set as active
    Texture* d_activeTexture;
    //! type to track info for per-texture sub batches of geometry
    typedef std::pair<LPDIRECT3DTEXTURE9, UINT> BatchInfo;
    //! type of container that tracks BatchInfos.
    typedef std::vector<BatchInfo> BatchList;
    //! list of texture batches added to the geometry buffer
    BatchList d_batches;
    //! type of container used to queue the geometry
    typedef std::vector<D3DVertex> VertexList;
    //! container where added geometry is stored.
    VertexList d_vertices;
    //! rectangular clip region
    hgeRect d_clipRect;
    //! translation vector
    Vector3 d_translation;
    //! rotation vector
    Vector3 d_rotation;
    //! pivot point for rotation
    Vector3 d_pivot;
    //! RenderEffect that will be used by the GeometryBuffer
    RenderEffect* d_effect;
    //! The D3D Device
    LPDIRECT3DDEVICE9 d_device;
    //! model matrix cache
    mutable D3DXMATRIX d_matrix;
    //! true when d_matrix is valid and up to date
    mutable bool d_matrixValid;
};

#if defined(_MSC_VER)
#   pragma warning(pop)
#endif

#endif  // end of guard _hgeGeometryBuffer_h_
